package br.com.treinar.modelo.principal;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import br.com.treinar.modelo.Contato;
import br.com.treinar.modelo.Telefone;
import br.com.treinar.modelo.TipoTelefone;

@SuppressWarnings("unchecked")
public class TesteExec {

	public static void main(String[] args) {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("treinar_modelo");
		EntityManager manager = factory.createEntityManager();
		manager.getTransaction().begin();
		Query q = manager.createQuery("from Contato");
		List<Contato> contatos = q.getResultList();
		Telefone t = null;
		Random gerador = new Random();
  
		for (Contato contato : contatos) {
			t = new Telefone();
			t.setDdd(gerador.nextInt(10) + 10);
			t.setNumero(gerador.nextInt(10000000) + 10000000);
			t.setTipoTelefone(TipoTelefone.CELULAR);
			//contato.setTelefones(new ArrayList<Telefone>());
			//contato.getTelefones().add(t);
			manager.refresh(contato);
		}
		
		for (Contato contato : contatos) {
			manager.merge(contato);
		}
		manager.getTransaction().commit();
		
		manager.close();
	}
	
}
